﻿using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SaleOnlineSystem.Admin.Controls
{
    public partial class EditImageAds : System.Web.UI.UserControl
    {
        public string image;
        public string link;
        public int id;
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                DataTable dt = DAL.DCollector.DBCollector.Instance.CallStored("callstored", "SELECT * FROM tbl_ImageAds", true);
                image = dt.Rows[0]["ImageAds_Image"].ToString();
                link = dt.Rows[0]["ImageAds_Link"].ToString();
                id = Convert.ToInt32(dt.Rows[0]["ImageAds_ID"].ToString());
            }
            catch (Exception)
            {
                Response.Redirect("/error.htm");
            }        
        }

        protected void btnUpdateAds_Click(object sender, EventArgs e)
        {
           if (fulImage.HasFile)
                {
                    if (LIB.Validate.CheckFileType(fulImage.FileName))
                    {

                        string link = fulImage.FileName;
                        LIB.Validate saveimage = new LIB.Validate();
                        image = "/Images/" + id + Path.GetExtension(fulImage.FileName) + "";
                        saveimage.ThayDoiKichThuocAnh(Server.MapPath("/Images/"), id + Path.GetExtension(fulImage.FileName), 980, fulImage.PostedFile.InputStream);

                        DAL.DHelpers.Column[] cl = new DAL.DHelpers.Column[3];
                        cl[0] = new DAL.DHelpers.Column("ImageAds_Image", image, DAL.DHelpers.DataType.DataTypeSql.Varchar);
                        cl[1] = new DAL.DHelpers.Column("ImageAds_Link", txtLink.Text, DAL.DHelpers.DataType.DataTypeSql.Varchar);
                        cl[2] = new DAL.DHelpers.Column("ImageAds_date", DateTime.Now.ToString(), DAL.DHelpers.DataType.DataTypeSql.Varchar);

                        string[] colWhere = new string[1];
                        colWhere[0] = "ImageAds_ID";

                        object[] colValue = new object[1];
                        colValue[0] = id;

                        DAL.DCollector.DBCollector.Instance.Update("tbl_ImageAds", cl, colWhere, colValue);
                        Response.Redirect("/Admin/Index.aspx?panel=5&sub=9");
                    }
                    else
                    {
                        Response.Write("<script>alert('Chỉ có thể upload file(.jpg,.png,.jpeg)')</script>");
                    }
                }
                else
                {
                    Response.Write("<script>alert('Bạn vui lòng upload ảnh...')</script>");
                }     
        }
    }
}